#ifndef __WMYSQLFUNC_CPP__
#define __WMYSQLFUNC_CPP__

#include "wmysql.h"
#include "wmysqlfunc.h"

using namespace WServer::MySQL;

namespace WMysqlFunc {

void msg_timeout(int clientindex, MsgBaseClass* pmsg)
{
	MsgTimeout* pmsgtimeout = (MsgTimeout*)pmsg;
}

void msg_loginattempt(int clientindex, MsgBaseClass* pmsg)
{
	MsgLoginAttempt* pmsgloginattempt = (MsgLoginAttempt*) pmsg;
	wserPrintMsg("loginattempt. username:%s key:%s\n", pmsgloginattempt->name.c_str(), pmsgloginattempt->key.c_str());

	char cmd[MAX_SIZE];WRecordset* record;
	sprintf(cmd, "SELECT * FROM player join user on user.id=player.userid WHERE user.name='%s' LIMIT 1", pmsgloginattempt->name.c_str());
	record = ExecuteQueryCmd(cmd);
	if (record->IsEOF()) {
		MsgLoginFail msgloginfail;msgloginfail.name = pmsgloginattempt->name;msgloginfail.key = pmsgloginattempt->key;
		CreateMessage2WMain(clientindex, msgloginfail);
		return;
	}

	MsgLoginSuccess msgloginsuccess;
	msgloginsuccess.playerid = atoi(record->GetFieldValue(0).c_str());

	msgloginsuccess.playerdata.playerName = record->GetFieldValue(2);
	msgloginsuccess.playerdata.playerLevel = atoi(record->GetFieldValue(3).c_str());
	msgloginsuccess.playerdata.playerExp = atoi(record->GetFieldValue(4).c_str());
	msgloginsuccess.playerdata.playerSex = atoi(record->GetFieldValue(5).c_str());
	FreeRecordset(record);

	sprintf(cmd, "SELECT * FROM playerItems WHERE parentID=%d ORDER BY itemPos", msgloginsuccess.playerid);
	record = ExecuteQueryCmd(cmd);
	msgloginsuccess.itemdata.resize(record->recordCount);
	for (int i = 0;i < record->recordCount;i++) {
		MsgLoginSuccessPlayerItem msgloginsuccessplayeritem;
		msgloginsuccessplayeritem.itemPos = atoi(record->GetFieldValue(2).c_str());
		msgloginsuccessplayeritem.itemID = atoi(record->GetFieldValue(3).c_str());
		msgloginsuccessplayeritem.args = record->GetFieldValue(4);
		msgloginsuccess.itemdata.push_back(msgloginsuccessplayeritem);
		record->MovetoNextRow();
	}

	sprintf(cmd, "SELECT * FROM playerTask WHERE parentID=%d ORDER BY taskID", msgloginsuccess.playerid);
	record = ExecuteQueryCmd(cmd);
	msgloginsuccess.taskdata.resize(record->recordCount);
	for (int i = 0;i < record->recordCount;i++) {
		MsgLoginSuccessPlayerTask msgloginsuccessplayertask;
		msgloginsuccessplayertask.taskID = atoi(record->GetFieldValue(2).c_str());
		msgloginsuccessplayertask.status = atoi(record->GetFieldValue(3).c_str());
		msgloginsuccessplayertask.atime = atoi(record->GetFieldValue(4).c_str());
		msgloginsuccessplayertask.stime = atoi(record->GetFieldValue(5).c_str());
		msgloginsuccess.taskdata.push_back(msgloginsuccessplayertask);
		record->MovetoNextRow();
	}

	sprintf(cmd, "SELECT * FROM playerCounter WHERE parentID=%d ORDER BY cid", msgloginsuccess.playerid);
	record = ExecuteQueryCmd(cmd);
	msgloginsuccess.counterdata.resize(record->recordCount);
	for (int i = 0;i < record->recordCount;i++) {
		MsgLoginSuccessPlayerCounter msgloginsuccessplayercounter;
		msgloginsuccessplayercounter.cid = atoi(record->GetFieldValue(2).c_str());
		msgloginsuccessplayercounter.args = record->GetFieldValue(3);
		msgloginsuccessplayercounter.c1 = atoi(record->GetFieldValue(4).c_str());
		msgloginsuccessplayercounter.c2 = atoi(record->GetFieldValue(5).c_str());
		msgloginsuccessplayercounter.c3 = atoi(record->GetFieldValue(6).c_str());
		msgloginsuccessplayercounter.ctime = atoi(record->GetFieldValue(7).c_str());
		msgloginsuccessplayercounter.utime = atoi(record->GetFieldValue(8).c_str());
		msgloginsuccess.counterdata.push_back(msgloginsuccessplayercounter);
		record->MovetoNextRow();
	}
	CreateMessage2WMain(clientindex, msgloginsuccess);
}

}

#endif //__WMYSQLFUNC_CPP__
